package com.baibei.pay.selector;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baibei.model.CityInfo;
import com.baibei.model.ProvinceInfo;
import com.baibei.pay.R;
import com.baibei.pay.R2;
import com.blankj.utilcode.utils.ScreenUtils;
import com.rae.widget.ScrollerNumberPicker;
import com.rae.widget.dialog.impl.SlideDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 开户省市选择器
 * Created by shenminjie on 2017/8/22.
 * mail:shenminjie@100bei.com
 */

public class ProvinceCityFragment extends DialogFragment {

    @BindView(R2.id.tv_cancle)
    TextView mTvCancle;
    @BindView(R2.id.tv_define)
    TextView mTvDefine;
    @BindView(R2.id.snp_province)
    ScrollerNumberPicker mSnpProvince;
    @BindView(R2.id.snp_city)
    ScrollerNumberPicker mSnpCity;
    @BindView(R2.id.container)
    LinearLayout mContainer;
    Unbinder unbinder;

    private List<ProvinceInfo> mProvinceInfos;
    /**
     * callback
     */
    private OnSelectListener<CityInfo> mListener;

    /**
     * 设置数据
     *
     * @param infos infos
     */
    public ProvinceCityFragment setInfos(List<ProvinceInfo> infos) {
        this.mProvinceInfos = infos;
        return this;
    }

    /**
     * 设置cb
     *
     * @param listener listener
     */
    public ProvinceCityFragment setListener(OnSelectListener<CityInfo> listener) {
        mListener = listener;
        return this;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_province_city, container, false);
        unbinder = ButterKnife.bind(this, view);
        ViewGroup.LayoutParams layoutParams = mContainer.getLayoutParams();
        layoutParams.width = ScreenUtils.getScreenWidth();
        mContainer.setLayoutParams(layoutParams);
        return view;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new SlideDialog(getContext());
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (mProvinceInfos != null && mProvinceInfos.size() != 0) {
            //设置数据
            mSnpProvince.setData(getKey());
            mSnpCity.setData(getValue(getKey().get(0)));
            mSnpProvince.setDefault(0);
            mSnpCity.setDefault(0);
            mSnpProvince.setOnSelectListener(new ScrollerNumberPicker.OnSelectListener() {
                @Override
                public void endSelect(int i, String s) {
                    //更新数据
                    mSnpCity.setData(getValue(s));
                    mSnpCity.setDefault(0);
                }

                @Override
                public void selecting(int i, String s) {

                }
            });
        }
    }

    @OnClick(R2.id.tv_cancle)
    public void onCancle() {
        if (mListener != null) {
            mListener.onCancle(this);
        }
    }

    @OnClick(R2.id.tv_define)
    public void onDefine() {
        if (mListener != null) {
            String cityName = mSnpCity.getSelectedText();
            CityInfo cityInfo = getCity(cityName);
            if (cityInfo != null) {
                mListener.onDefineListener(cityInfo, this);
            }
        }
    }

    /**
     * 获取省市
     *
     * @return return
     */
    private ArrayList<String> getKey() {
        ArrayList<String> key = new ArrayList<>();
        for (ProvinceInfo provinceInfo : mProvinceInfos) {
            key.add(provinceInfo.getName());
        }
        return key;
    }

    /**
     * 获取值
     *
     * @param province province
     * @return return
     */
    private ArrayList<String> getValue(String province) {
        ProvinceInfo provinceInfo = null;
        for (ProvinceInfo temp : mProvinceInfos) {
            if (temp.getName().equals(province)) {
                provinceInfo = temp;
                break;
            }
        }
        ArrayList<String> values = new ArrayList<>();
        if (provinceInfo != null) {
            for (CityInfo cityInfo : provinceInfo.getCityInfos()) {
                values.add(cityInfo.getName());
            }
        }
        return values;
    }

    /**
     * 获取城市
     *
     * @param name name
     * @return return
     */
    private CityInfo getCity(String name) {
        for (ProvinceInfo temp : mProvinceInfos) {
            for (CityInfo cityInfo : temp.getCityInfos()) {
                if (cityInfo.getName().equals(name)) {
                    return cityInfo;
                }
            }
        }
        return null;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
